package com.xiaoman.jvm.classpath;

import com.xiaoman.jvm.classpath.impl.CompositeEntry;
import com.xiaoman.jvm.classpath.impl.DirEntry;
import com.xiaoman.jvm.classpath.impl.WildcardEntry;
import com.xiaoman.jvm.classpath.impl.ZipEntry;

import java.io.IOException;

/**
 * 表示类路径项
 */
public interface Entry {
    String pathListSeparator = System.getProperty("path.separator");

    /**
     * 寻找和加载class文件
     *
     * @param className class文件的相对路径，路径之间用斜线（/）分隔，文件名有.class后缀
     * @return 返回值是读取到的字节数据、最终定位到class文件的Entry，以及错误信息
     */
    byte[] readClass(String className) throws IOException;

    /**
     * 返回变量的字符串表示
     *
     * @return
     */
    String String();

    static Entry newEntry(String path) {
        if (path.contains(pathListSeparator)) {
            return new CompositeEntry(path);
        } else if (path.endsWith("*")) {
            return new WildcardEntry(path);
        } else if (path.endsWith(".jar") || path.endsWith(".JAR") || path.endsWith(".zip") || path.endsWith(".ZIP")) {
            return new ZipEntry(path);
        } else {
            return new DirEntry(path);
        }
    }
}
